package com.lindroy.androidplayer.ui.mine

import android.os.Bundle
import androidx.fragment.app.viewModels
import com.lindroy.androidplayer.R
import com.lindroy.androidplayer.base.BaseFragment
import com.lindroy.androidplayer.databinding.FragmentLibraryBinding
import com.lindroy.androidplayer.ui.adapter.LibraryAdapter
import com.lindroy.androidplayer.ui.webview.WebViewFragment
import com.lindroy.androidplayer.utils.addSimpleDivider
import com.lindroy.androidplayer.viewmodels.LibraryItemViewModel
import com.lindroy.androidplayer.viewmodels.LibraryViewModel

/**
 * @author Lin
 * @date 2022/3/3
 * @function 开源许可
 */
class LibraryFragment : BaseFragment<FragmentLibraryBinding>() {

    private val viewModel by viewModels<LibraryViewModel>()

    private val adapter by lazy { LibraryAdapter(viewLifecycleOwner) }

    private val libraries = hashMapOf(
        "OKHttp" to "https://github.com/square/retrofit",
        "Retrofit" to "https://github.com/square/retrofit",
        "Coil" to "https://github.com/coil-kt/coil/blob/master/README-zh.md",
        "EasyEditText" to "https://github.com/lindroy/EasyEditText",
        "Linkage-RecyclerView" to "https://github.com/KunMinX/Linkage-RecyclerView",
        "MultiStateLayout" to "https://github.com/lindroy/MultiStateLayout",
        "BannerViewPager" to "https://github.com/zhpanvip/BannerViewPager",
        "xLog" to "https://github.com/elvishew/xLog",
        "AgentWeb" to "https://github.com/Justson/AgentWeb",
        "PersistentCookieJar" to "https://github.com/franmontiel/PersistentCookieJar",
        "MMKV" to "https://github.com/Tencent/MMKV",
        "LoggingInterceptor" to "https://github.com/ihsanbal/LoggingInterceptor"
    )


    override fun FragmentLibraryBinding.initView(savedInstanceState: Bundle?) {
        initToolbar(R.string.about_open_source)
        rvLibrary.addSimpleDivider()
        rvLibrary.adapter = adapter
    }

    override fun initData() {
        viewModel.setData(libraries)
    }

    override fun FragmentLibraryBinding.initListener() {
        viewModel.libraryListLiveData.observe(viewLifecycleOwner) {
            adapter.setData(it)
        }
        viewModel.itemClickLiveData.observe(viewLifecycleOwner) {
            WebViewFragment.start(this@LibraryFragment, title = it.name, url = it.url)
        }
    }

}